package net.cyl.strategy;

import net.cyl.emums.BizCodeEnum;
import net.cyl.exception.BizException;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

public class CustomDBPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Comparable<?>> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Comparable<?>> preciseShardingValue) {
        String codePrefix = preciseShardingValue.getValue().toString().substring(0, 1);
        for(String tableName : collection){
            String targetName =tableName.substring(tableName.length()-1);
            if(targetName.equals(codePrefix)){
                return tableName;
            }
        }
        throw new BizException(BizCodeEnum.DB_ROUTER_NOT_EXIST);
    }
}
